/**
 * Filename: TodayShareActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/6/29 2:57
 */
package com.github.postsanf.yinian.activity;

import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.pop.PopListAlbum;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNStatusResponse;
import com.github.postsanf.yinian.entity.YNUpTokenResponse;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.SnackBarUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.utils.Log;

import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

/**
 * 今日忆分享页
 */
public class TodayShareActivity2 extends BaseActivity implements PopListAlbum.OnImageAlbumSelected, View.OnClickListener {
    private UploadManager mUpManager;
    private Toolbar mToolbar;
    private ImageView btn_share_local;
    private ImageView btn_share_yinian;
    private ImageView btn_share_wx_timeline;
    private ImageView btn_share_wx_friend;
    private ImageView btn_share_qq_timeline;
    private ImageView btn_share_qq;
    private String TMpic;
    private String TMtext;
    private int CurrentItem;
    private View view;
    private List<YNAlbum> mDatas;
    private String nameStr;
    private UMShareAPI mShareAPI;
    private PopListAlbum mPopListAlbumPopupWindow;        // 弹窗相册路径
    private int mScreenHeight;                    // 屏高
    private ProgressDialog mProgressDialog;
//    Handler downloadHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case 2:
//                    SnackBarUtils.makeShort(mToolbar, "保存成功！").show();
//                    break;
//                case 1:
//                    SnackBarUtils.makeShort(mToolbar, "网络连接失败！").show();
//                    break;
//                case 0:
//                    SnackBarUtils.makeShort(mToolbar, "保存失败！").show();
//                    break;
//            }
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_today_share);
        mUpManager = application.getUploadManager();
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setMessage(CommonConstants.TIP_LOAD_UP);
        TMpic = getIntent().getStringExtra("TMpic");
        TMtext = getIntent().getStringExtra("TMtext");
        CurrentItem = getIntent().getIntExtra("CurrentItem", 0);
        Log.i("xjs", "TMpic==>" + TMpic);
        mDatas = YNSingleton.getInstance().getAlbumList();
        mScreenHeight = DisplayUtils.getScreenHeightPixels(this);
        mShareAPI = UMShareAPI.get(this);
        initView();
        initListAlbumPopupWindw();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.today_share_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        view = findViewById(R.id.root_today_share);
        btn_share_local = (ImageView) findViewById(R.id.btn_share_local);
        btn_share_yinian = (ImageView) findViewById(R.id.btn_share_yinian);
        btn_share_wx_timeline = (ImageView) findViewById(R.id.btn_share_wx_timeline);
        btn_share_wx_friend = (ImageView) findViewById(R.id.btn_share_wx_friend);
        btn_share_qq_timeline = (ImageView) findViewById(R.id.btn_share_qq_timeline);
        btn_share_qq = (ImageView) findViewById(R.id.btn_share_qq);
        btn_share_local.setOnClickListener(this);
        btn_share_yinian.setOnClickListener(this);
        btn_share_wx_timeline.setOnClickListener(this);
        btn_share_wx_friend.setOnClickListener(this);
        btn_share_qq_timeline.setOnClickListener(this);
        btn_share_qq.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 保存至本地
            case R.id.btn_share_local:
                SnackBarUtils.makeShort(mToolbar, "正在下载...").show();
                getUrlBitmap(TMpic, TMtext, CurrentItem, 1);
                break;
            // 保存到忆年相册
            case R.id.btn_share_yinian:
                mProgressDialog.show();
                getUrlBitmap(TMpic, TMtext, CurrentItem, 2);
                Intent statintentyinian = new Intent(ACTIONs.record);
                statintentyinian.putExtra("key", "todayMemoryYinian_6");
                statintentyinian.putExtra("value", 1);
                sendBroadcast(statintentyinian);
                break;
            // 朋友圈
            case R.id.btn_share_wx_timeline:
                getUrlBitmap(TMpic, TMtext, CurrentItem, 3);
                Intent statintent = new Intent(ACTIONs.record);
                statintent.putExtra("key", "todayMemoryFriendCircle_6");
                statintent.putExtra("value", 1);
                sendBroadcast(statintent);
                break;
            // 微信好友
            case R.id.btn_share_wx_friend:
                getUrlBitmap(TMpic, TMtext, CurrentItem, 4);
                Intent statintentfriend = new Intent(ACTIONs.record);
                statintentfriend.putExtra("key", "todayMemoryWechat_6");
                statintentfriend.putExtra("value", 1);
                sendBroadcast(statintentfriend);
                break;
            // qq空间
            case R.id.btn_share_qq_timeline:
                getUrlBitmap(TMpic, TMtext, CurrentItem, 5);
                Intent statintentzone = new Intent(ACTIONs.record);
                statintentzone.putExtra("key", "todayMemoryQZone_6");
                statintentzone.putExtra("value", 1);
                sendBroadcast(statintentzone);
                break;
            case R.id.btn_share_qq:
                getUrlBitmap(TMpic, TMtext, CurrentItem, 6);
                Intent statintentqq = new Intent(ACTIONs.record);
                statintentqq.putExtra("key", "todayMemoryQQ_6");
                statintentqq.putExtra("value", 1);
                sendBroadcast(statintentqq);
                break;
        }
    }

    Bitmap Picbitmap;
    String Pictext;
    int Picposition = 0;

    public void getUrlBitmap(final String url, final String text, final int position, final int type) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL iconUrl = new URL(url);
                    URLConnection conn = iconUrl.openConnection();
                    HttpURLConnection http = (HttpURLConnection) conn;
                    int length = http.getContentLength();
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is, length);
                    Picbitmap = BitmapFactory.decodeStream(bis);
                    bis.close();
                    is.close();// 关闭流
                    Pictext = text;
                    Picposition = position;
                    bitmapHandler.sendEmptyMessage(type);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


    Handler bitmapHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                //保存本地
                case 1:
                    Bitmap res1 = getResPhoto();
                    saveBitmapres(res1);
                    SnackBarUtils.makeShort(mToolbar, "保存成功！").show();
                    break;
                case 2:
                    Bitmap res2 = getResPhoto();
                    saveBitmapresAndUpload(res2);
                    break;
                case 3:
                    Bitmap res3 = getResPhoto();
                    new ShareAction(TodayShareActivity2.this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                            .withMedia(new UMImage(TodayShareActivity2.this, res3))
                            .share();
                    break;
                case 4:
                    Bitmap res4 = getResPhoto();
                    new ShareAction(TodayShareActivity2.this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                            .withText("今日忆")
                            .withMedia(new UMImage(TodayShareActivity2.this, res4))
                            .share();
                    break;
                case 5:
                    Bitmap res5 = getResPhoto();
                    new ShareAction(TodayShareActivity2.this).setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
                            .withText("今日忆")
                            .withMedia(new UMImage(TodayShareActivity2.this, res5))
                            .share();
                    break;
                case 6:
                    Bitmap res6 = getResPhoto();
                    new ShareAction(TodayShareActivity2.this).setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
                            .withText("今日忆")
                            .withMedia(new UMImage(TodayShareActivity2.this, res6))
                            .share();
                    break;
            }
        }
    };

    boolean filebb = false;
    private MediaScannerConnection mMediaonnection;
    String ts;
    File f;

    public void saveBitmapres(Bitmap bm) {
        filebb = true;
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "yinian/YiNian");
        if (!file.exists()) {
            file.mkdirs();
        }
        ts = "yn_" + System.currentTimeMillis() + ".jpg";
        f = new File(file.getPath() + "/", ts);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
//            Toast.makeText(TodayShareActivity2.this, "已保存", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!f.exists())
            return;
        ContentValues values = new ContentValues(7);
        values.put(MediaStore.Images.Media.TITLE, ts);
        values.put(MediaStore.Images.Media.DISPLAY_NAME, ts);
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.DATA, f.getAbsolutePath());
        getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        // 最后通知图库更新
        try {
            mMediaonnection = new MediaScannerConnection(TodayShareActivity2.this, new MediaScannerConnection.MediaScannerConnectionClient() {
                @Override
                public void onMediaScannerConnected() {
                    mMediaonnection.scanFile(f.getParentFile().getAbsolutePath(), ts);
                }

                @Override
                public void onScanCompleted(String path, Uri uri) {
                    mMediaonnection.disconnect();
                }
            });
            mMediaonnection.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    String jieTuUrl;

    public void saveBitmapresAndUpload(Bitmap bm) {
        filebb = true;
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "yinian/YiNian");
        if (!file.exists()) {
            file.mkdirs();
        }
        ts = "yn_" + System.currentTimeMillis() + ".jpg";
        f = new File(file.getPath() + "/", ts);
        jieTuUrl = f.getAbsolutePath();
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
//            Toast.makeText(TodayShareActivity2.this, "已保存", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!f.exists())
            return;
        mProgressDialog.dismiss();
        chooseGroup();
    }

    // 获取上传Token
    private void getTokenCover(final int giiid) {
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynGetPrivateSpaceUploadToken))
                .setMethod(HttpMethods.Post)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNUpTokenResponse resToken = gson.fromJson(s, YNUpTokenResponse.class);
                        if (ReqUtils.isSuccess(resToken.getCode())) {
                            String prefix = jieTuUrl.substring(jieTuUrl.lastIndexOf(".") + 1);
                            final String imgName = UUID.randomUUID().toString() + "." + prefix;
                            mUpManager.put(jieTuUrl, imgName, resToken.getData().get(0).getToken(),
                                    new UpCompletionHandler() {
                                        @Override
                                        public void complete(String key, ResponseInfo info, JSONObject res) {

                                        }
                                    }, new UploadOptions(null, null, false, new UpProgressHandler() {
                                        public void progress(String key, double percent) {
                                            Log.i("xjs", "UploadOptions");
                                            if (percent == 1.0) {
                                                doUpPic(key, String.valueOf(giiid));
                                            }
                                        }
                                    }, null));
                        } else {
                            mProgressDialog.dismiss();
                            showToast(resToken.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        mProgressDialog.dismiss();
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    public static final String[] mouthlist = new String[]{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

    private Bitmap getResPhoto() {
        int width = 750;
        int height = 1334;
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        canvas.drawColor(Color.WHITE);
        if (Picbitmap != null) {
            Rect dst = new Rect();
            dst.left = 0;
            dst.top = 0;
            dst.right = 750;
            dst.bottom = 936;
            canvas.drawBitmap(Picbitmap, null, dst, null);
        }
        Rect dstkuang = new Rect();
        dstkuang.left = 305;
        dstkuang.top = 300;
        dstkuang.right = 305 + 140;
        dstkuang.bottom = 300 + 272;
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.todayshow_kuang), null, dstkuang, null);
        Rect dstline = new Rect();
        dstline.left = 0;
        dstline.top = 936 - 157;
        dstline.right = 750;
        dstline.bottom = 936;
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.splash_white_line), null, dstline, null);
        String splash_year;
        String splash_mouth;
        String splash_day;
        String day;
        if (Picposition == 0) {
            Date yesterdate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
            day = new SimpleDateFormat("yyyy-MM-dd").format(yesterdate);
            splash_mouth = mouthlist[yesterdate.getMonth()];
        } else {
            Date mdate = new Date(new Date().getTime());
            day = new SimpleDateFormat("yyyy-MM-dd").format(mdate);
            splash_mouth = mouthlist[mdate.getMonth()];
        }
        splash_year = day.split("-")[0];
        splash_day = day.split("-")[2];
        Paint p1 = new Paint();
        p1.setColor(Color.parseColor("#ffffff"));
        p1.setAntiAlias(true);
        p1.setFilterBitmap(true);
        p1.setTextSize(76f);
        p1.setTextAlign(Paint.Align.CENTER);
        p1.setTypeface(Typeface.createFromAsset(getAssets(), "splash_day.ttf"));
        canvas.drawText(splash_day, 375, 396, p1);
        p1.setTextSize(26f);
        canvas.drawText(splash_year, 375, 498, p1);
        p1.setTextSize(30f);
        p1.setTypeface(Typeface.createFromAsset(getAssets(), "splash_mouth.ttf"));
        canvas.drawText(splash_mouth, 375, 444, p1);
        p1.setTextSize(28f);
        p1.setColor(Color.parseColor("#636363"));
        canvas.drawText("@忆年APP", 375, 1260, p1);
        TextPaint textPaint = new TextPaint();
        textPaint.setColor(Color.parseColor("#333333"));
        textPaint.setTextSize(38.0F);
        textPaint.setTypeface(Typeface.createFromAsset(getAssets(), "MFKeSong-Regular.ttf"));
        String ttt = "";
        if (!StringUtils.isEmpty(TMtext)) {
            ttt = TMtext;
        } else {
            ttt = "我遇见你,我记得你,这座城市天生就适合谈恋爱,你天生就适合我的灵魂你天生就适合我的灵魂你天生就适合我的灵魂";
        }
        if (ttt.length() >= 55) {
            ttt = ttt.substring(0, 54) + "...";
        }
        StaticLayout layout = new StaticLayout(ttt, textPaint, 530, Layout.Alignment.ALIGN_CENTER, 1.3F, 0.0F, true);
        canvas.translate(120, 975);
        layout.draw(canvas);
        return result;
    }

//    private void downPic(final String url, final String name) {
//        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "yinian/YiNian");
//        if (!(file.mkdir() || file.isDirectory())) {
//            file.mkdirs();
//        }
//        new Thread(new Runnable() {
//            public void run() {
//                try {
//                    URL url1 = new URL(url);
//                    HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
//                    conn.setConnectTimeout(5000);
//                    conn.setRequestMethod("GET");
//                    int code = conn.getResponseCode();
//                    if (code == 200) {
//                        InputStream is = conn.getInputStream();
//                        File f = new File(new File(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/", "yinian/YiNian" + name);
//                        if (f.exists()) {
//                            f.delete();
//                        }
//                        FileOutputStream fos = new FileOutputStream(f);
//                        int len = 0;
//                        byte[] buff = new byte[1024];
//                        while ((len = is.read(buff)) != -1) {
//                            fos.write(buff, 0, len);
//                        }
//                        is.close();
//                        fos.close();
//                        downloadHandler.sendEmptyMessage(2);
//                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//                        Uri uri = Uri.fromFile(f);
//                        intent.setData(uri);
//                        sendBroadcast(intent);
//                    } else {
//                        downloadHandler.sendEmptyMessage(1);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    downloadHandler.sendEmptyMessage(0);
//                }
//            }
//        }).start();
//    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(TodayShareActivity2.this, platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(TodayShareActivity2.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                finish();
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(TodayShareActivity2.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(TodayShareActivity2.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    private void chooseGroup() {
//        if (TMpic.length() <= 0) return;
//        nameStr = TMpic.substring(TMpic.lastIndexOf("/") + 1);
        if (mDatas.size() <= 0) {
            showToast("请先创建圈子再发布");
            return;
        }
        mPopListAlbumPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        // 设置背景颜色变暗
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = .8f;
        getWindow().setAttributes(lp);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }

    // 选择发布相册的弹框初始化
    private void initListAlbumPopupWindw() {
        mPopListAlbumPopupWindow = new PopListAlbum(
                ViewGroup.LayoutParams.MATCH_PARENT, (int) (mScreenHeight * 0.5),
                mDatas, LayoutInflater.from(this)
                .inflate(R.layout.yn_album_list_dir, null));
        mPopListAlbumPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 设置背景颜色
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1.0f;
                getWindow().setAttributes(lp);
            }
        });
        mPopListAlbumPopupWindow.setOnImageAlbumSelected(this);
    }

    // 选择相册回调
    @Override
    public void selected(YNAlbum item) {
        getTokenCover(item.getGroupid());
        mPopListAlbumPopupWindow.dismiss();
    }


    private String getCurrentTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    private void doUpPic(String key, String curGroupid) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        String urlsto = "https://api.zhuiyinanian.com/YinianProject/event/UploadEvent";
        mParam.put("userid", application.getCurID() + "");
        mParam.put(CommonConstants.YNS_GROUP_ID, curGroupid);
        mParam.put(CommonConstants.YNS_CONTENT, "");
        mParam.put(CommonConstants.YNS_PIC_ADDRESS, key);
        mParam.put(CommonConstants.YNS_TOTALNUM, "");
        mParam.put(CommonConstants.YNS_TOTALMONEY, "");
        mParam.put(CommonConstants.YNS_AUDIO, "");
        mParam.put(CommonConstants.YNS_PLACE, "");
        mParam.put(CommonConstants.YNS_MAIN, "0");
        mParam.put(CommonConstants.YNS_SOURCE, "app");
        mParam.put(CommonConstants.YNS_STORAGE, "100");
        Log.i("xjs", "urlsto===>" + urlsto);
        StringRequest req = new StringRequest(urlsto)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNStatusResponse resStatus = gson.fromJson(s, YNStatusResponse.class);
                        if (ReqUtils.isSuccess(resStatus.getCode())) {
                            Log.i("xjs", "uploadsssssss==>" + s.toString());
                            if (resStatus.getData() != null) {
                                YNStatus mstatus = resStatus.getData().get(0);
                                YNSingleton.getInstance().clearCurStatus();
                                YNSingleton.getInstance().setCurStatus(mstatus);
                                // 通知动态更新
                                Intent mIntent = new Intent(ACTIONs.actionStatus);
                                mIntent.putExtra(ACTIONs.IsAdd, true);
                                mIntent.putExtra(ACTIONs.StatusId, mstatus.getEid());
                                sendBroadcast(mIntent);
                                sendBroadcast(new Intent(ACTIONs.actionAlbumInit));
                                sendBroadcast(new Intent(ACTIONs.actionGridWallUpdate));
                                showToast("成功");
                            }
                        } else {
                            showToast(resStatus.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Log.i("xjs", "onFailure");
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);

    }
//    private void doUpPic(String curGroupid) {
//        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
//        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
//        mParam.put(CommonConstants.YNS_GROUP_ID, curGroupid);
//        mParam.put(CommonConstants.YNS_CONTENT, " ");
//        mParam.put(CommonConstants.YNS_PIC_ADDRESS, CommonConstants.YNQNURL + nameStr);
//        mParam.put(CommonConstants.YNS_PIC_MODE, "private");
//        mParam.put(CommonConstants.YNS_MEMORYTIME, getCurrentTime());
//        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynUploadEvent))
//                .setMethod(HttpMethods.Post)
//                .setParamMap(mParam)
//                .setHttpListener(new HttpListener<String>(true, false, true) {
//                    @Override
//                    public void onSuccess(String s, Response<String> response) {
//                        YNStatusResponse resStatus = gson.fromJson(s, YNStatusResponse.class);
//                        if (ReqUtils.isSuccess(resStatus.getCode())) {
//                            showToast("发布成功");
//                            finish();
//                        } else {
//                            showToast(resStatus.getMsg());
//                        }
//                    }
//
//                    @Override
//                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
//                    }
//                });
//        liteHttp.executeAsync(req);
//    }


    // 友盟统计
    public void onResume() {
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        super.onResume();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
